<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: IntroduceController.php 2010-07-02 19:54 mirlan $
 * @author     Mirlan
 */

/**
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Inviter_FacebookController extends Core_Controller_Action_Standard {
	public function indexAction() {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$inviterApi = Engine_Api::_ ()->getApi ( 'core', 'inviter' );
		$facebook = Inviter_Api_Provider::getFBInstance ();
		$this->view->appId = $facebook->getAppId ();
		
		if (! $viewer->getIdentity () || ! $this->view->appId) {
			$this->_redirect ( 'inviter' );
		}
		
		$this->view->navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'inviter_profile', array (), 'inviter_facebook' );
		
		$host_url = (_ENGINE_SSL ? 'https://' : 'http://') . $_SERVER ['HTTP_HOST'];
		$invite_code = substr ( md5 ( rand ( 0, 999 ) . $viewer->getIdentity () ), 10, 7 );
		
		$this->view->action_url = $host_url . $this->view->url ( array ('skip' => 1, 'code' => $invite_code ), 'inviter_contacts', true );
		$this->view->invite_url = $host_url . $this->view->url ( array ('module' => 'inviter', 'controller' => 'signup', 'code' => $invite_code ), 'default', true );
		
		$this->view->exclude_ids = '';
		$this->view->init_fb_app = $inviterApi->checkInitFbApp ();
		
		try {
			$fb_user_id = Inviter_Api_Provider::getFBUserId ();
			if ($fb_user_id) {
				$exclude_ids = $inviterApi->getAlreadyMemberFbFriends ( $fb_user_id );
				$this->view->exclude_ids = ($exclude_ids) ? implode ( ',', $exclude_ids ) : '';
			}
		} catch ( Exception $e ) {
		}
	}
	
	public function userContentAction() {
		$user_id = array_pop ( explode ( '_', $this->_getParam ( 'id', '' ) ) );
		
		if (! $user_id) {
			$this->view->error = 1;
			$this->view->message = 'Error happened.';
			return;
		}
		
		$inviterApi = Engine_Api::_ ()->getApi ( 'core', 'inviter' );
		$this->view->user = $user = Engine_Api::_ ()->getItem ( 'user', $user_id );
		
		$this->view->mutual_friend_count = $inviterApi->getMutualFriendCount ( $user->user_id );
		$this->view->mutual_like_count = $inviterApi->getMutualLikeCount ( $user->user_id );
		
		if ($this->view->mutual_like_count) {
			$params = array ('poster_type' => $user->getType (), 'poster_id' => $user->getIdentity () );
			
			$select = Engine_Api::_ ()->like ()->getLikesSelect ( $params );
			$select->where ( 'like1.resource_type IN ("page", "user")' );
			
			$this->view->likedMembersAndPages = Engine_Api::_ ()->like ()->getTable ()->fetchAll ( $select )->count ();
		}
		
		if ($this->view->mutual_friend_count) {
			$this->view->paginator = $paginator = $inviterApi->getMutualFriends ( array ('user_id' => $user_id ) );
			$paginator->setItemCountPerPage ( 4 );
		}
		
		$this->view->html = $this->view->render ( '_user_content.tpl' );
	}
	
	public function joinAction() {
		$session = new Zend_Session_Namespace ( 'inviter' );
		
		$formSkipped = $this->_getParam ( 'skip_inviter', false );
		$formSubmitted = $this->_getParam ( 'submitFacebook', false );
		$contactIds = $this->_getParam ( 'ids', array () );
		$session->__set ( 'inviterStep', 'sendInvitation' );
		
		$inviterApi = Engine_Api::_ ()->getApi ( 'core', 'inviter' );
		/**
		 * @var $providerApi Inviter_Api_Provider
		 */
		$providerApi = Engine_Api::_ ()->getApi ( 'provider', 'inviter' );
		
		$facebook = Inviter_Api_Provider::getFBInstance ();
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->appId = $facebook->getAppId ();
		$this->view->init_fb_app = $inviterApi->checkInitFbApp ();
		
		$host_url = (_ENGINE_SSL ? 'https://' : 'http://') . $_SERVER ['HTTP_HOST'];
		$this->view->invite_code = $invite_code = substr ( md5 ( rand ( 0, 999 ) . $viewer->getIdentity () ), 10, 7 );
		$this->view->action_url = $host_url . $this->view->url ( array ('skip_inviter' => 1 ), 'inviter_facebook_signup' );
		$this->view->invite_url = $host_url . $this->view->url ( array ('module' => 'inviter', 'controller' => 'signup', 'code' => $invite_code ), 'default', true );
		$this->view->exclude_ids = '';
		
		if ($formSkipped && $formSubmitted) {
			$this->view->submitForm = true;
			$this->view->skipForm = true;
			$this->view->form = $form = new Inviter_Form_Signup_Contacts ();
			$form->setAction ( $this->view->url ( array (), 'user_signup' ) );
			
			$this->view->contactIds = implode ( ',', $contactIds );
			$session->__set ( 'custom_submit_fb', true );
			$session->__set ( 'invite_code', $invite_code );
			
			return;
		
		} elseif ($formSkipped) {
			$this->view->skipForm = true;
			$this->view->form = $form = new Inviter_Form_Signup_Contacts ();
			$form->setAction ( $this->view->url ( array (), 'user_signup' ) );
			
			return;
		}
		
		$this->view->show_login_btn = true;
		
		if ($this->view->appId) {
			try {
				$fbUserInfo = $facebook->api ( '/me' );
				if ($fbUserInfo) {
					$this->view->show_login_btn = false;
				}
			} catch ( Exception $e ) {
				return;
			}
		}
		
		try {
			$fb_user_id = Inviter_Api_Provider::getFBUserId ();
			
			if ($fb_user_id) {
				$exclude_ids = $inviterApi->getAlreadyMemberFbFriends ( $fb_user_id );
				$this->view->exclude_ids = ($exclude_ids) ? implode ( ',', $exclude_ids ) : '';
			}
		} catch ( Exception $e ) {
			$this->view->exclude_ids = '';
		}
	}
}